package impl;

import dao.OrderAndDetailSelect;
import util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @classname:OrderAndDetail
 * @description:
 * @author: Mr.Z
 * @date: 2021/1/2 20:14
 * @version: 0.1
 * @since:1.8
 */
public class OrderAndDetailSelectImpl implements OrderAndDetailSelect {
    private Connection conn;
    private String sql;
    private PreparedStatement ps;
    private ResultSet rs;
    private int result;
    @Override
    public List<Map<String, Object>> selectOrderAndDetailByGoodId(int id) {
        conn = DBHelper.getCon();
        List<Map<String, Object>> list = new ArrayList<>(10);
        sql = "SELECT g.id,g.gname,o.uid,o.total,d.count from goodinfo g,orderinfo o,detail d\n" +
                " where g.id = ? and g.id =d.id and d.uid=o.uid";
        try {
           ps=  conn.prepareStatement(sql);
           ps.setInt(1,id);
           rs = ps.executeQuery();
            while (rs.next()) {
                Map<String, Object> map = new LinkedHashMap<>(16);
                map.put("id", rs.getInt("id"));
                map.put("gname", rs.getObject("gname"));
                map.put("uid", rs.getObject("uid"));
                map.put("total", rs.getObject("total"));
                map.put("count", rs.getObject("count"));
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBHelper.closeResources(conn,ps,rs);
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> selectOrderAndDetailByMember(int mid) {
        conn = DBHelper.getCon();
        List<Map<String, Object>> list = new ArrayList<>(10);
        sql = "SELECT m.mid,m.mname,o.total,o.paytype,d.count from member m,orderinfo o,detail d where m.mid = ? and m.mid =o.mid and d.uid=o.uid";
        try {
            ps=  conn.prepareStatement(sql);
            ps.setInt(1,mid);
            rs = ps.executeQuery();
            while (rs.next()) {
                Map<String, Object> map = new LinkedHashMap<>(16);
                map.put("mid", rs.getInt("mid"));
                map.put("mname", rs.getObject("mname"));
                map.put("total", rs.getObject("total"));
                map.put("paytype", rs.getObject("paytype"));
                map.put("count", rs.getObject("count"));
                map.put("count", rs.getObject("count"));
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBHelper.closeResources(conn,ps,rs);
        }
        return list;
    }
}
